// pages/Main/mainIndex/mainIndex.js
var businessLogic = require('../../../utils/NetWork/Model/LoginbusinessLogic.js')
var common = require('../../../utils/common.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectRole: true,
    firstRole: '请选择登录角色',
    selectArea: false,
     content:[],
     content2:[],
     itemData2: [],
     orgid: "",
     userid: "",
     openid: "",
     nickName:"",
     rid: "",
     tiaoid:"",
     username:"请授权您的微信号"
  },
 

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.tiaoid);
    //如果是0 代表是启动页进了跳转个人中心 如果是1 代表是绑定也进了跳转
    this.setData({
      tiaoid: options.tiaoid
    })
  },
  getUserInfo: function (e) {
    console.log(e)
    this.setData({
      userInfo: e.detail.userInfo,
      username: e.detail.userInfo.nickName,
      hasUserInfo: true
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  userNameInput: function (e) {
    console.log(e.detail.value)
    this.setData({
      userName: e.detail.value
    })
  },
  passWdInput: function (e) {
    this.setData({
      userPwd: e.detail.value
    })
  },
  //获取用户输入的密码
  loginBtnClick: function (e) {
    // console.log("用户名：" + this.data.userName + " 密码：" + this.data.userPwd);
    if (this.data.userName == undefined || this.data.userPwd == undefined) {
      common.showToast("请输入账号和密码", "none")
    } else {

      let params = { "username": this.data.userName, password: this.data.userPwd };
      businessLogic.userLogin(params, (res) => {
        console.log(res);
        if (res.flag == 0) {
          common.showToast(res.msg, "none");
        } else {
          common.showToast(res.msg, "success");
          var userId = res.userId;
          common.setStorageSync("userid", res.userId);
          var orgid = res.orgid;
          common.setStorageSync("orgid", res.orgid);
          var rid = res.rid;
          common.setStorageSync("rid", res.rid);
          var user = common.getStorageSync("user");
          var openid=common.getStorageSync("openid");
          let openindex = common.getStorageSync("openindex");
          console.log(user);
          console.log(openid);
          var oppen = "";
          if (openindex == "" || openindex==undefined){
            if (user==undefined){
              oppen = openid;
            }else{
              oppen = user[0].openid + "||||" + openid;
            }
          }else{
            oppen = ""+user[openindex].openid+"||||"+openid+"";
          }
          var n = oppen.split("||||");
          oppen = common.unique(n);
          oppen = oppen.join('||||');
          let InfoJson = '{ "userid": "' + userId + '", "orgid": "' + orgid + '", "id": "' + userId + '", "openid": "'+oppen+'"}'
          let params2 = { "orgid": orgid, "userid": userId, "InfoJson": InfoJson }
      //     //第三步 调用接口
          businessLogic.bangding(params2, (res) => {
            console.log(res);
         
            // wx.reLaunch({
            //   url: '../../Login/logining/logining?id=' + this.data.content + '&username=' + this.data.userName + '&password=' + this.data.userPwd,
            // })  

          
             wx.reLaunch({
               url: '../../Main/teacherOrder/teacherOrder',
            })  

          });
          
        }
        

      });

    }

  },
  list:function () {

    // 第二步 传参
   
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }, register: function (e) {
    wx.navigateTo({
      url: '../register/register',
    })
  }, forgetPWD: function (e) {
    wx.navigateTo({
      url: '../forgetpassword/forgetpassword',
    })
  }, 
  clickRole: function () {
    var selectRole = this.data.selectRole;
    if (selectRole == true) {
      this.setData({
        selectArea: true,
        selectRole: false,
      })
    } else {
      this.setData({
        selectArea: false,
        selectRole: true,
      })
    }
  }, 
  mySelect: function (e) {
    this.setData({
      firstRole: e.target.dataset.me,
      selectRole: true,
      selectArea: false,
    })
  },
  send: function (e) {


  },

  // getUserInfo: function (e) {
   

  // //   console.log("1");
  // //  // console.log(e.detail.signature)
  // //  //app.globalData.userInfo = e.detail.userInfo
  // //  console.log(e)
  // //   this.setData({
  // //     userInfo: e.detail.userInfo,
  // //     hasUserInfo: true,
  // //     content: e.detail.signature
  // //   })
  // } 
})